﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.Teachers.Dtos;
using HOPU.OnlineSchool.Core;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HOPU.OnlineSchool.Application.Teachers.Services
{
    public class TeacherService : ITeacherService, ITransient
    {
        private readonly IRepository<EduTeacher> _teacherrepostory;

        public TeacherService(IRepository<EduTeacher> teacherrepostory)
        {
            _teacherrepostory = teacherrepostory;
        }

        /// <summary>
        /// 添加讲师信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddAsync(TeacherAddInput input)
        {
            var model = input.Adapt<EduTeacher>();
            // 需要对id进行赋值
            //通过雪花算法生成主键id
            model.Id = YitIdHelper.NextId().ToString();
            await _teacherrepostory.InsertAsync(model);
        }

        /// <summary>
        /// 删除讲师
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(string id)
        {
            var model = await _teacherrepostory.FirstOrDefaultAsync(e => e.IsDeleted == 0 && e.Id == id);
            if (model == null)
                throw Oops.Oh("该讲师已经被删除");
            model.IsDeleted = 1;
            model.GmtModified = DateTime.Now;

            //更新指定的字段信息
            await _teacherrepostory.UpdateIncludeAsync(model, new string[] { nameof(model.IsDeleted), nameof(model.GmtModified) });
        }

        /// <summary>
        /// 分页查询讲师信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="listInput"></param>
        /// <returns></returns>
        public async Task<PagedList<TeacherListOutput>> GetPagedListAsync(int page, int limit, TeacherListInput listInput)
        {
            var query = _teacherrepostory.Where(t => t.IsDeleted == 0);
            if (!string.IsNullOrWhiteSpace(listInput.Name))
                query = query.Where(e => e.Name.Contains(listInput.Name));
            if (listInput.Level>0)
                query = query.Where(e => e.Level==listInput.Level);
            if (listInput.Begin.HasValue)
                query = query.Where(e => e.GmtCreate >= listInput.Begin.Value);
            if (listInput.End.HasValue)
                query = query.Where(e => e.GmtCreate <= listInput.End.Value);

            var list = await query.OrderByDescending(f => f.GmtCreate).ToPagedListAsync(page, limit);
            //          Mapster使用 对象关系映射
            return list.Adapt<PagedList<TeacherListOutput>>();
        }

        /// <summary>
        /// 获取编辑讲师信息id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TeacherDto> GetTeacherAsync(string id)
        {
            var model = await _teacherrepostory.FirstOrDefaultAsync(t => t.Id == id && t.IsDeleted == 0);
            if (model == null)
                throw Oops.Oh("该讲师未找到");
            return model.Adapt<TeacherDto>();
        }

        /// <summary>
        /// 更新讲师
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task UpdateAsync(string id, TeacherDto dto)
        {
            var exists = await _teacherrepostory.AnyAsync(t => t.Id == id && t.IsDeleted == 0);
            if (!exists)
                throw Oops.Oh("该讲师未找到");
            var teacher = dto.Adapt<EduTeacher>();
            await teacher.UpdateExcludeAsync(new[] { nameof(teacher.GmtCreate), nameof(teacher.IsDeleted) });
        }
    }
}

